package com.bookstore.mapper;

import com.bookstore.domain.Book;
import com.bookstore.domain.ShoppingCart;
import com.bookstore.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface ShoppingCartMapper {
    @Select("SELECT s.*,b.bookName,b.price FROM SHOPPING_CART s,book b WHERE s.bid=b.bid and s.uid=#{uid}")
    @Results({
            @Result(property = "uid",
                    javaType = User.class,
                    column = "uid",
                    one = @One(select = "com.bookstore.mapper.UserMapper.findByUid")
            ),
            @Result(property = "bid",
                    javaType = Book.class,
                    column = "bid",
                    one = @One(select = "com.bookstore.mapper.BookMapper.findById")
            ),
    })
    List<ShoppingCart> findAllByUid(@Param("uid") Integer uid);

    //获取要结算付款的订单
    @Select("SELECT s.*,b.bookName,b.price FROM SHOPPING_CART s,book b WHERE s.bid=b.bid and s.uid=#{uid} and s.bid=#{bid}")
    @Results({
            @Result(property = "uid",
                    javaType = User.class,
                    column = "uid",
                    one = @One(select = "com.bookstore.mapper.UserMapper.findByUid")
            ),
            @Result(property = "bid",
                    javaType = Book.class,
                    column = "bid",
                    one = @One(select = "com.bookstore.mapper.BookMapper.findById")
            ),
    })
    ShoppingCart findAllByUidAndBid(@Param("uid") Integer uid,@Param("bid") Integer bid);

    @Delete("delete from SHOPPING_CART  where uid=#{uid} and bid=#{bid}")
    int delShopping(@Param("uid") Integer uid, @Param("bid")Integer bid);

    //添加新的购物车信息
    @Insert("insert into SHOPPING_CART values(#{uid},#{bid},#{count})")
    int addCart(@Param("bid") Integer bid, @Param("uid")Integer uid, @Param("count")Integer count);

    @Delete("delete from SHOPPING_CART where uid=#{uid} and bid=#{bid}")
    int delCart(@Param("bid") Integer bid, @Param("uid")Integer uid);
    //修改商品数量 重复加购的时候
    @Update("update shopping_cart set count=#{count} where uid=#{uid}" +
            "" +
            " and bid=#{bid}")
    int upCount(@Param("uid") Integer uid, @Param("bid")Integer bid, @Param("count")Integer count);
}
